package com.shuyuntu.proposal.service;

import com.shuyuntu.proposal.dao.IProposalDao;
import com.shuyuntu.proposal.dao.ISystemConfigDao;
import com.shuyuntu.proposal.entity.Proposal;
import com.shuyuntu.proposal.entity.SystemConfig;
import com.shuyuntu.proposal.enums.EProposalState;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 用来给整个系统提供若干预制信息
 *
 * Created by limeng on 16/5/16.
 */
@Service
public class SystemConfigService {

    @Transactional
    public SystemConfig getSystemConfigById(int configId) {
        return systemConfigDao.queryById(configId);
    }

    /**
     * 获取最新一次的大会系统信息
     * @return ...
     */
    @Transactional
    public SystemConfig getLatestConfig() {
        SystemConfig config = systemConfigDao.queryFirst(Order.desc("id"));
        return config;
    }

    /**
     * 手动关闭一次大会
     * @return
     */
    @Transactional
    public int close() {
        try {
            SystemConfig config = getLatestConfig();
            if (config.getMotionClosingTime() == null) {
                return 3;
            }
            Date nowDate = new Date();
            if (nowDate.before(config.getEndTime())) {
                config.setEndTime(nowDate);
                systemConfigDao.update(config);
                return 0;
            } else {
                return 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 2;
        }
    }

    /**
     * 关闭最近一次的提案附议
     * @return 0 : 成功, 1 : 大会已经结束了, 2 : 数据库遇到问题
     */
    @Transactional
    public int closeMotion() {
        try {
            SystemConfig config = getLatestConfig();
            Date nowDate = new Date();
            if (nowDate.before(config.getEndTime())) {
                config.setMotionClosingTime(nowDate);
                systemConfigDao.update(config);
                //操作文章
                if (handleCloseMotion()) {
                    return 0;
                } else {
                    return 2;
                }
            } else {
                return 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 2;
        }
    }

    @Transactional
    public boolean newSystemConfig(SystemConfig newConfig) {
        //FIXME:这里的判断待验证
        if (systemConfigDao.insert(newConfig) != 0) {
            return true;
        }
        return false;
    }

    @Transactional
    private boolean handleCloseMotion() {
        try {
            //TODO:如果性能瓶颈,可以空间换时间
            SystemConfig config = getLatestConfig();
            List<Proposal> list = proposalDao.queryList(
                    Restrictions.eq("proposalState", EProposalState.WAIT_SECOND_MOTION)
                    //TODO:去掉了检查创建时间的条件
                    //Restrictions.gt("createTime", config.getStartTime())
            );
            for (Proposal proposal : list) {
                if (proposal.getSecondMotions().size() >= 3) {
                    proposal.setProposalState(EProposalState.UNREGISTERED);
                } else {
                    proposal.setProposalState(EProposalState.INVALID);
                }
                proposalDao.update(proposal);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Transactional
    public List<SystemConfig> queryHistoryWithPagination(
            int pageSize, int pageNo, StringBuffer rowCount) {
        Order order = Order.desc("endTime");
        return systemConfigDao.queryPageList(pageNo, pageSize, rowCount, order);
    }

    /*********依赖注入:*********/
    @Resource
    ISystemConfigDao systemConfigDao;
    @Resource
    IProposalDao proposalDao;

    public ISystemConfigDao getSystemConfigDao() {
        return systemConfigDao;
    }

    public void setSystemConfigDao(ISystemConfigDao systemConfigDao) {
        this.systemConfigDao = systemConfigDao;
    }

    public IProposalDao getProposalDao() {
        return proposalDao;
    }

    public void setProposalDao(IProposalDao proposalDao) {
        this.proposalDao = proposalDao;
    }



}
